home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / builtin2.zoo / compare.c < prev    next >
C/C++ Source or Header  |  1988-08-15  |  3KB  |  107 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /* compare.c */
  26.  
  27. #include "builtin.h"
  28.  
  29. extern float floatval();
  30.  
  31. b_COMPARE() /* R1, R2: two terms to be compared; R3: free var to set the 
  32.         result of the comparison: R1 < R2: int<0,  R1 = R2: 0, >0 o/w.
  33.         R3 must be free and a LOCAL new variable */
  34.         /* LIST is the largest term of the all four kinds */
  35. {
  36.     if (!unify(gregc(3), makeint(compare(gregc(1), gregc(2))))) {Fail0;}
  37. }
  38.  
  39. compare(val1, val2)
  40. word val1, val2;
  41. {
  42.         register pw top;
  43.     int a, b, c;
  44.     struct psc_rec *ptr1, *ptr2;
  45.     float fpt_val;
  46.  
  47.     deref(val2);
  48.     cd: switch ((int)(val1&3)) {
  49.       case FREE:
  50.         nderef(val1, cd);
  51.         if (!isnonvar(val2)) return(val1 - val2);
  52.         else return -1;
  53.       case NUM:
  54.         if (!isnonvar(val2)) return 1;
  55.         else if (isinteger(val1) && isinteger(val2))
  56.             return intval(val1) - intval(val2);
  57.         else if (isfloat(val1) && isfloat(val2)) {
  58.         fpt_val = floatval(val1) - floatval(val2);
  59.         if (fpt_val > 0) return 1;
  60.         else if (fpt_val == 0) return 0;
  61.         else return -1;
  62.         }
  63.         else return -1;
  64.       case CS:
  65.         if (!isnonvar(val2) || isinteger(val2)) return 1;
  66.         else if (islist(val2)) return -1;
  67.         else {
  68.         ptr1 = get_str_psc(val1);
  69.         ptr2 = get_str_psc(val2);
  70.             a = get_arity(ptr1);
  71.             b = get_arity(ptr2);
  72.         if (a != b) return a-b;
  73.         c = comalpha(ptr1, ptr2);
  74.         if (c || (a == 0)) return c;
  75.         untag(val1);
  76.         untag(val2);
  77.         for (b = 1; b <= a; b++) {
  78.             c = compare(follow(((pw)val1)+b), follow(((pw)val2)+b));
  79.             if (c) break;
  80.         }
  81.         return c;
  82.         }
  83.         /* break; */
  84.       case LIST:
  85.         if (!islist(val2)) return 1;
  86.         else {
  87.         untag(val1);
  88.         untag(val2);
  89.         c = compare(follow(val1), follow(val2));
  90.         if (c) return c;
  91.         else return compare(follow(((pw)val1)+1),follow(((pw)val2)+1));
  92.         }
  93.         /* break; */
  94.     }
  95. }
  96.  
  97. comalpha(name1, name2)
  98. struct psc_rec *name1, *name2;
  99. {
  100.     char s1[256], s2[256];
  101.  
  102.     if (name1 == name2) return 0;
  103.     namestring(name1, s1);
  104.     namestring(name2, s2);
  105.     return strcmp(s1, s2);
  106. }
  107.